using System;
using System.IO;

using System.Collections.Generic;
using System.Text;
using Fan.Dongping.DataAccess;
using Fan.Dongping.Common.DBMeta;
using Fan.Dongping.Common;

namespace Fan.Dongping.BusinessRules
{
    /// <summary>
    /// 
    /// </summary>
    public class FileBiz
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="fileSize"></param>
        /// <param name="description"></param>
        /// <param name="fileType"></param>
        /// <param name="uploadUser"></param>
        /// <param name="bugNo"></param>
        /// <returns></returns>
        public static int UploadFile(string fileName, long fileSize, string description, string fileType, string uploadUser, long bugNo)
        {
            int fileNo = FileDA.UploadAFile(fileName, fileSize, description, fileType, uploadUser);
            FileDA.CreateFileBugRelation(fileNo, bugNo, uploadUser);
            return fileNo;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="originalFileName"></param>
        /// <returns></returns>
        public static string CreateFilePath(string originalFileName)
        {
            string path = string.Format("\\{0}\\{1}\\{2}\\", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);

            string tmpSubFileName = path + originalFileName;

            string fileFirst = System.IO.Path.GetFileNameWithoutExtension(originalFileName);
            string fileExtension = System.IO.Path.GetExtension(originalFileName);

            int i = 0;
            while (File.Exists(Config.Instance.DirectoryOfUpload + tmpSubFileName) == true)
            {
                tmpSubFileName = path + fileFirst + i + fileExtension;
                i++;
            }

            // path = ;
            if (Directory.Exists(Config.Instance.DirectoryOfUpload + path) == false)
            {
                Directory.CreateDirectory(Config.Instance.DirectoryOfUpload + path);
            }

            return tmpSubFileName;
        }

    }
}
